package test.n00300;


import static utils.Tools.listOf;

public class Problem00380 {
    public static void main(String[] args) {
        println(5);
    }

    public static void println(int n) {
        Solution ss = new Solution();
        System.out.println(listOf(n) + "," + listOf(ss.countAndSay(n)));
    }

    private static class Solution {
        public String countAndSay(int n) {
            String str = "1";
            for (int i = 2; i <= n; i++) {
                StringBuilder sb = new StringBuilder();
                int start = 0;
                int pos = 0;

                while (pos < str.length()) {
                    while(pos < str.length() && str.charAt(start) == str.charAt(pos)) {
                        pos++;
                    }
                    sb.append(pos - start).append(str.charAt(start));
                    start = pos;
                }

                str = sb.toString();
            }
            return str;
        }
    }
}
